﻿Imports Mysoft.Map.Data
Imports System.IO

Partial Class UpFile
    Inherits System.Web.UI.Page

#Region " Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object
    Private strSql As String

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '在此处放置初始化页的用户代码
        Dim sDocType As String = Request.QueryString("DocType")

        If Not Me.IsPostBack Then
            FKGUID.Text = Request.QueryString("FkGUID")
            OnAfterUpFile.Text = Request.QueryString("OnAfterUpFile")
            OnReady.Text = Request.QueryString("OnReady")
            Dim sTitle As String = Request.QueryString("Title")
            If sTitle <> "" Then
                lblInfo.Text = sTitle
            End If
            Dim strWhere As String

            If FKGUID.Text = "" Then
                strWhere = "1=2"
                FKGUID.Text = Guid.NewGuid().ToString()
            Else
                strWhere = "FkGUID='" & FKGUID.Text & "' AND DocType='" & sDocType & "'"
            End If

            strSql = "SELECT *,CASE WHEN CreateOn IS NOT NULL AND CreateOn<>'' THEN CONVERT(char(20), CreateOn ,20) ELSE '' END AS CreateOn1" & _
                    " FROM ep_Documents" & _
                    " WHERE " & strWhere & " ORDER BY CreateOn DESC"
        Else
            IsUpFile.Text = "1"
            BackPara.Text = ""

            Select Case OperateType.Text
                Case "1"
                    SaveFile(sDocType)
                Case "2"
                    SaveURL(sDocType)
                Case "3"
                    DelFile(sDocType)
            End Select

        End If

        If Request.QueryString("mode") = "3" Then
            IsLock.Text = "1"
        End If
        strSql = "SELECT DocGUID as oid,DocName" & _
                        ",CASE WHEN FileName<>'' THEN Location + FileName ELSE URL END AS url" & _
                        " FROM ep_Documents" & _
                        " WHERE FkGUID='" & FKGUID.Text & "' AND DocType='" & sDocType & "' ORDER BY CreateOn DESC"
        Dim dtTemp As DataTable = MyDB.GetDataTable(strSql)
        FileCount.Text = dtTemp.Rows.Count
        Repeater1.DataSource = dtTemp
        Repeater1.DataBind()
    End Sub

    '保存文件
    Private Sub SaveFile(ByVal sDocType As String)
        Dim sYMD As String = Today.Year.ToString & Today.Month.ToString & Today.Day.ToString
        Dim sShortFileName As String = GetShortFileName(txtUpFile.Value)

        Dim sFilesPath As String = Server.MapPath("/UpFiles/" & sYMD & "/")
        Dim sFileExpName As String = Mid(sShortFileName, sShortFileName.LastIndexOf(".") + 1)
        Dim sFileName As String = sDocType & sYMD & Now.Hour.ToString & Now.Minute.ToString & Now.Second.ToString & sFileExpName

        Try
            If Not Directory.Exists(sFilesPath) Then
                Directory.CreateDirectory(sFilesPath)
            End If
            upfile.PostedFile.SaveAs(sFilesPath & sFileName)
        Catch ex As Exception
            BackPara.Text = ex.Message
            Return
        End Try

        strSql = "INSERT INTO p_Documents (DocGUID" & _
                                        ",DocType" & _
                                        ",DocName" & _
                                        ",FileName" & _
                                        ",URL" & _
                                        ",jd_documentDetailType" & _
                                        ",Location" & _
                                        ",FkGUID" & _
                                        ",CreateBy" & _
                                        ",CreateOn" & _
                                ") VALUES (newid()" & _
                                        ",'" & sDocType & "'" & _
                                        ",'" & sShortFileName & "'" & _
                                        ",'" & sFileName & "'" & _
                                        ",''" & _
                                        ",'成本系统'" & _
                                        ",'/UpFiles/" & sYMD & "/'" & _
                                        ",'" & FKGUID.Text & "'" & _
                                        ",newid()" & _
                                        ",getdate()" & _
                                ")"
        If MyDB.ExecSQL(strSql) < 0 Then
            File.Delete(sFilesPath & sFileName)
            BackPara.Text = "插入文档数据失败,请关闭重试!"
        End If
    End Sub

    '保存历史记录
    Private Sub SaveURL(ByVal sDocType As String)
        Dim sURL As String = Url.Text

        strSql = "INSERT INTO p_Documents (DocGUID" & _
                                        ",DocType" & _
                                        ",DocName" & _
                                        ",FileName" & _
                                        ",URL" & _
                                        ",jd_documentDetailType" & _
                                        ",Location" & _
                                        ",FkGUID" & _
                                        ",CreateBy" & _
                                        ",CreateOn" & _
                                ") VALUES (newid()" & _
                                        ",'" & sDocType & "'" & _
                                        ",'" & sURL & "'" & _
                                        ",''" & _
                                        ",'" & sURL & "'" & _
                                        ",'成本系统'" & _
                                        ",''" & _
                                        ",'" & FKGUID.Text & "'" & _
                                        ",newid()" & _
                                        ",getdate()" & _
                                ")"
        If MyDB.ExecSQL(strSql) < 0 Then
            BackPara.Text = "插入文档数据失败,请关闭重试!"
        Else
            Url.Text = ""
        End If
        upfile.Disabled = False
        txtUpFile.Disabled = False
    End Sub

    '删除文件
    Private Sub DelFile(ByVal sDocType As String)
        If DelList.Text <> "" Then
            strSql = "DELETE FROM p_Documents WHERE DocGUID IN (" & DelList.Text & ")"
            If MyDB.ExecSQL(strSql) < 0 Then
                BackPara.Text = "删除失败,请关闭重试!"
            Else
                DelList.Text = ""
            End If
        Else
            BackPara.Text = "请选中要删除项!"
        End If

    End Sub

    Private Function GetShortFileName(ByVal strFileName As String) As String
        Dim i As Integer = strFileName.LastIndexOf("\")
        If i < 0 Then
            Return strFileName
        Else
            Return strFileName.Substring(i + 1)
        End If
    End Function
End Class
